package com.ruoyi.sms.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.sms.component.SmsComponent;
import org.redisson.spring.cache.RedissonCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/sms")
public class SmsSendController {
    @Autowired
    private SmsComponent smsComponent;

    @Anonymous
    @GetMapping("/sendCode")
    public R sendCode(@RequestParam("phone") String phone){
        Random random = new Random();
//        String code = redisTemplate.opsForValue().get(phone).toString();

        String code="";
            for (int i = 0; i < 6; i++) {

                code+=random.nextInt(9);
            }
        String key = CacheConstants.CAPTCHA_CODE_KEY + phone;


//        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        RedisUtils.setCacheObject(key, code, false);
        smsComponent.sendSmsCode(phone,code);




        return R.ok();
    }
}
